<template>
  <div class="roles">
    <div class="header">
      <!-- 打卡 -->
      <router-link to="/Backstage" class="shou">首页</router-link> / 权限管理 / 角色列表
    </div>
    <el-button class="tjjs" @click="dialogFormVisible=true">添加角色</el-button>
    <!-- 添加角色表单 -->
    <div>
      <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
        <el-form :model="form" ref="form" class="demo-ruleForm">
          <el-form-item
            :label-width="formLabelWidth"
            label="角色名称"
            prop="name"
            :rules="[
              { required: true, message: '名称不能为空'}
            ]"
          >
            <el-input v-model="form.name" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" :label-width="formLabelWidth">
            <el-input v-model="form.miao"></el-input>
          </el-form-item>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="jsqx">取 消</el-button>
          <el-button type="primary" :plain="true" @click="jstj('form')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 中间框 -->
    <div class='biaoge'>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">          
            <el-row v-for="(i,k) in props.row.children" :key="k">
              <el-col :span="4">
                <el-tag closable @close="handleClosea(i.id,props.row.id)">{{i.authName}}</el-tag>
                <i class="el-icon-arrow-right"></i>
              </el-col>
              <el-col :span="20">
                <el-row v-for="(_i,_k) in i.children" :key="_k">
                  <el-col :span="4">
                    <el-tag closable @close="handleCloseb(_i.id,props.row.id)">{{_i.authName}}</el-tag>
                    <i class="el-icon-arrow-right"></i>
                  </el-col>
                  <el-col :span="20">
                   <el-col :span="2.5" v-for="(_it,_kt) in _i.children" :key="_kt">                   
                      <el-tag closable @close="handleClosec(_it.id,props.row.id)">{{_it.authName}}</el-tag>                    
                    </el-col>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" width="180"></el-table-column>
        <el-table-column prop="mg_state" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="fang(scope.row.username,scope.row.email,scope.row.mobile,scope.row.id)"></el-button>
            <el-button type="success" icon="el-icon-delete" circle @click="shan(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-check" circle @click="fenpei"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 删除 -->
    <div class="shan_chu">
      <el-dialog
        title="提示"
        :visible.sync="dialogFormVisibleb"
        width="30%"
        center>
        <span>此操作将永久删除该文件，是否继续？</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="scqx">取 消</el-button>
          <el-button type="primary" @click="qdsc">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 节点树 -->
    <div class="jied_shu">
      <el-dialog title="提示" :visible.sync="dialogFormVisiblec" width="30%">
      <el-tree
        :data="data4"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :expand-on-click-node="false"
        :render-content="renderContent">
      </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisiblec = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisiblec = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script> 
let id = 1000;
import axios from 'axios';
import {tjjs,jslb,scjs,zdjs,qxlb} from '../../api/api.js'
export default {
    name:'roles',
    data() {
      return {
        dialogFormVisible:false,
        dialogFormVisibleb:false,
        dialogFormVisiblec:false,
        formLabelWidth: '120px',
        form: {
          name: '',
          miao:'',
          id:''
        },
        tableData:[],
        data4: [],
        defaultProps: {
          children: 'children',
          label: 'authName'
        }
      }
    },
    methods:{
      //角色列表
      xina(){
        jslb().then(res=>{
          console.log(res)
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            this.tableData=res.data.data
          }
          console.log('角色列表',this.tableData)
        })
      },
      //添加角色
      tian(){
        tjjs({
          roleName:this.form.name,
          roleDesc:this.form.miao
        }).then(res=>{
          console.log(res)
        })
      },
      //添加角色确定按钮
      jstj(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.dialogFormVisible = false;
            this.tian();
            this.xina();
            this.$message({
              showClose: true,
              message: '恭喜您，添加成功',
              type: 'success'
            });
          } else {
            this.$message({
              showClose: true,
              message: '信息未填完噢',
              type: 'warning'
            });
            return false;
          }
        });
      },
      //添加角色取消按钮
      jsqx(){
        this.dialogFormVisible = false;
        this.$message({
          showClose: true,
          message: '取消成功',
          type: 'success'
        });
      },
      //点击删除按钮
      shan(id){
        this.dialogFormVisibleb=true;
        this.form={
          id:id
        }
      },
      //点击删除确定
      qdsc(){
        this.dialogFormVisibleb=false;
        scjs({
          id:this.form.id
        }).then(res=>{
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            console.log('删除成功',res);
            this.xina();
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            });
          }
        })
      },
      //点击删除取消
      scqx(){
        this.dialogFormVisibleb=false;
        this.$message({
          showClose: true,
          message: '已取消',
          type: 'warning'
        });
      },
      //点击分配
      fenpei(){
        this.dialogFormVisiblec=true
        this.qxlba()
        // this.$nextTick(()=>{
        //   this.$refs.tree.tableData(row.children);
        // });
      },
      //节点树这些玩意
      append(data) {
        const newChild = { id: id++, label: 'testtest', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },

      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      },

      renderContent(h, { node, data, store }) {
        return (
          <span style="flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px;">
            <span>
              <span>{node.label}</span>
            </span>
          </span>
          );
      },
      //节点树点击删除一层
      handleClosea(i,pro) {
        // console.log(i)
        // console.log(pro)
        zdjs({
          rightId:i,
          roleId:pro
        }).then(res=>{
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            this.xina();
            this.$message({
              showClose: true,
              message: '成功移除权限',
              type: 'success'
            });
          }
        })
      },
      //节点树点击删除二层
      handleCloseb(i,pro){
        console.log(i);
        console.log(pro)
        zdjs({
          rightId:i,
          roleId:pro
        }).then(res=>{
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            this.xina();
            this.$message({
              showClose: true,
              message: '成功移除权限',
              type: 'success'
            });
          }
        })
      },
      //节点树点击删除三层
      handleClosec(i,pro){
        console.log(i);
        console.log(pro)
        zdjs({
          rightId:i,
          roleId:pro
        }).then(res=>{
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            this.xina();
            this.$message({
              showClose: true,
              message: '成功移除权限',
              type: 'success'
            });
          }
        })
      },
      //权限列表
      qxlba(){
        qxlb({
          type:"tree"
        }).then(res=>{
          console.log('权限列表',res)
          if(res.data.meta.status>=200&&res.data.meta.status<300){
            this.data4=res.data.data
          }
          // console.log('qqqq',this.data4)
        })
      }
    },
    created(){
      this.xina();
    }
  };
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.roles{
  height:832px;
  width:1620px;
  background-color: #fff;
  border:1px solid #E9EEF3;
  box-shadow: 0 0 10px #ccc;
  border-radius: 5px;
  font-size: 14px;
  padding: 20px;
  overflow:scroll;
  overflow-x:auto
}
.header{
  color:#606266;
  letter-spacing:2px;
}
.shou{
  font-weight: bold;
  color:black;
}
.tjjs{
  margin-top: 10px;
}
</style>
